﻿#pragma once
#include "io/RandomAccessFile.h"
#include "yzrutil.h"
#include "io/ByteOrder.h"

namespace yzrilyzr_util{
	EBCLASS(WAVWriter){
	public:
	static constexpr int const FORMAT_PCM=1;
	static constexpr int const FORMAT_FLOAT=3;
	static constexpr int const PCM_HEADER_LENGTH=44;
	static constexpr int const FLOAT_HEADER_LENGTH=56;
	yzrilyzr_io::RandomAccessFile raf;
	int32_t frames=0;
	private:
	int32_t byteRate;
	int32_t blockAlign;
	int32_t numChannels;
	int32_t bitsPerSample;
	int32_t bytePerSample;
	int32_t sampleRate;
	int32_t format;
	public:
	WAVWriter(const yzrilyzr_lang::String & file) :raf(file, "rw"){
		raf.outputOrder(yzrilyzr_io::ByteOrder::LITTLE_ENDIAN);
	}
	void prepare(int32_t sampleRate, int32_t bitsPerSample, int32_t format, int32_t numChannels);
	void writeFloat(float v);
	void writeDouble(double v);
	void writeShort(int16_t v);
	void writeByte(int8_t v);
	void nextFrame();
	void end();
	};
}